package com.xnew.utils;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import com.alibaba.fastjson.JSONObject;

/**
 * create HttpEntity
 * @author yangcd
 * restTemplate 请求传递头部信息
 * */
public class HttpEntityUtil {
	public static HttpEntity<JSONObject> createHttpEntity(HttpServletRequest request, JSONObject postData){
        //拿到header信息
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
          String key = (String) headerNames.nextElement();
          String value = request.getHeader(key);
          requestHeaders.add(key, value);
        }
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(postData, requestHeaders);
        return  requestEntity;
    }
}
